<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
        <el-table :data="list" stripe style="width: 100%">
          <el-table-column prop="stem" label="标题" width="400">
          </el-table-column>
          <el-table-column prop="creator" label="作者"> </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间" width="200">
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #default="{ row }">
              <div class="actions">
                <i
                  class="el-icon-view"
                  @click="openDrawer('preview', row.id)"
                ></i>
                <i
                  class="el-icon-edit-outline"
                  @click="openDrawer('edit', row.id)"
                ></i>
                <i class="el-icon-delete" @click="del(row.id)"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        background
        layout="prev,pager,next,total,jumper"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
      :title="drawerTitle"
      direction="rtl"
      size="50%"
    >
      <!-- 预览使用 -->
      <div v-if="drawerType === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
      <el-form
        v-else
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit" type="primary">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticleList,
  delArticleItem,
  createArticle,
  getArticleDetail,
  updateArticle,
} from "@/api/article";

import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";
export default {
  data() {
    return {
      list: [],
      current: 1,
      pageSize: 10,
      total: 1,
      // 控制模态框的显示与隐藏
      isShowDrawer: false,
      // 控制模态框大标题的文字
      drawerType: "",
      form: {
        stem: "",
        content: "",
      },
      rules: {
        stem: [{ required: true, message: "请输入面经标题", trigger: "blur" }],
        content: [
          { required: true, message: "请输入面经标题", trigger: "blur" },
        ],
      },
      id: "",
    };
  },
  created() {
    this.initData();
  },
  methods: {
    // 处理页面变化，请求新数据
    handleCurrentChange(current) {
      this.current = current;
      this.initData();
    },
    // 删除某个数据
    async del(id) {
      try {
        await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delArticleItem(id);
        this.$message.success("删除成功");
        this.initData();
      } catch (error) {
        this.$message.error("取消删除");
      }
    },
    // 初始化数据，获取数据
    async initData() {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize,
      });
      const { rows, total } = res.data;
      this.list = rows;
      this.total = total;
      // console.log(res);
    },
    // 打开模态框
    async openDrawer(type, id) {
      if (type === "edit" || type === "preview") {
        const res = await getArticleDetail(id);
        const { stem, content } = res.data;
        this.form.stem = stem;
        this.form.content = content;
        if (type === "edit") {
          this.id = id;
        }
      }
      this.isShowDrawer = true;
      this.drawerType = type; //显示抽屑组件
    },
    // 关闭模态框  重置表单
    handleClose() {
      this.isShowDrawer = false;
      // this.$refs.form.resetFields();
    },
    submit() {
      // 验证表单
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let msg = "";
          if (this.id) {
            await updateArticle({ ...this.form, id: this.id });
            msg = "恭喜，编辑成功！";
            this.id = "";
          } else {
            await createArticle(this.form); //新增api
            msg = "恭喜，添加成功！";
          }
          this.handleClose(); //关闭表单方法、清空表单
          this.$nextTick(() => {
            // this.$refs.form.resetFields(); //重置表单
            this.form = {
              stem: "",
              content: "",
            };
          });
          this.current = 1; //设置current为1使页面跳回第一页
          await this.initData(); //更新表单
          this.$message.success(msg); //输出提示
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {
    drawerTitle() {
      let title = "大标题";
      if (this.drawerType === "add") title = "添加面经";
      if (this.drawerType === "preview") title = "面经预览";
      if (this.drawerType === "edit") title = "修改面经";
      return title;
    },
  },
  components: {
    quillEditor,
  },
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>

